<template>
  <div id="" ImageUpload>
    <!--
      action 必传 需要给一个后端接口地址, elementui会帮你发送请求
      我们不需要它自动发请求, 我们将来自己发送请求, 此时给空 不让他报错

      file-list 文件列表数组

      on-preview 当你点击预览的+号时, 会自动触发
    -->
    <el-upload
      :class="{ disabled: isDisabled }"
      action=""
      list-type="picture-card"
      :file-list="fileList"
      :on-preview="handlePreview"
      :on-remove="handleRemove"
      :http-request="handleUpload"
      :on-change="handleChange"
      :before-upload="handleBeforeUpload"
    >
      <i class="el-icon-plus" />
    </el-upload>
    <el-dialog
      title="图片预览"
      :visible="showDialog"
      @close="showDialog = false"
    >
      <img style="display: block; width: 100%" :src="imgUrl" alt="">
    </el-dialog>
  </div>
</template>

<script>
import COS from 'cos-js-sdk-v5' // 导入腾讯云的包(sdk)

const cos = new COS({
  SecretId: 'AKIDhAPN5VgaDYcPvA7oFPN5WgbGemdCVYKo', // 身份识别ID
  SecretKey: 'ECtKTMzLPDk0yh9KDqCHuIRIsCH5csqY' // 身份秘钥
})
export default {
  name: 'ImageUpload',
  props: {
    limit: {
      type: Number,
      default: 1
    }
  },
  data() {
    return {
      showDialog: false,
      imgUrl: '',
      fileList: [
        // {
        //   url: 'https://img1.baidu.com/it/u=3755297117,609162545&fm=26&fmt=auto'
        // },
        // {
        //   url: 'https://hrsass-xp-1308196647.cos.ap-shanghai.myqcloud.com/612769doge.gif'
        // },
        // {
        //   url: 'https://img0.baidu.com/it/u=907215057,771896613&fm=26&fmt=auto'
        // }
      ]
    }
  },
  computed: {
    isDisabled() {
      return this.fileList.length >= this.limit
    },
    isAllUploadSuccess() {
      return this.fileList.every((item) => item.status === 'success')
    }
  },
  methods: {
    handlePreview(file) {
      this.showDialog = true
      console.log(file)
      this.imgUrl = file.url
    },
    handleRemove(file, fileList) {
      console.log(file, fileList, this.fileList)
      this.fileList = this.fileList.filter((item) => item.uid !== file.uid)
    },
    handleUpload(obj) {
      console.log('Go for it!')
      console.log(obj)

      const uid = obj.file.uid
      const fileObj = this.fileList.find(item => item.uid === uid)
      fileObj.status = 'uploading'
      cos.putObject({
        Bucket: 'hrsass-xp-1308196647', // 存储桶名字
        Region: 'ap-shanghai', // 存储桶所在地域
        Key: parseInt(Math.random() * 999999) + obj.file.name, // 上传到存储桶中的文件名
        StorageClass: 'STANDARD', // 设置对象存储类型: 标准
        Body: obj.file, // 上传的文件对象
        // 箭头函数的this => 组件
        // 每上传一部分都会触发这个函数, 更新进度条
        onProgress: (progressData) => {
          console.log(JSON.stringify(progressData))
          // this.percent = parseInt(progressData.percent * 100)
          fileObj.percentage = parseInt(progressData.percent * 100)
        }
      }, (err, data) => {
        // 注意使用箭头函数,避免this问题(this => 组件)
        // 成功或者失败走这里
        console.log(this)
        console.log(err || data)
        fileObj.url = 'https://' + data.Location
        setTimeout(() => {
          fileObj.status = 'success'
        }, 1000)
      })
    },
    handleChange(file, fileList) {
      this.fileList = [...fileList]
    },
    handleBeforeUpload(file) {
      const isLt2M = file.size / 1024 / 1024 < 2
      if (!isLt2M) {
        this.$message.error('啊，太大了，达咩~')
        return false
      }
      return true
    }
  }
}
</script>
<style lang="scss" scoped>
.disabled {
  ::v-deep {
    .el-upload--picture-card {
      display: none;
    }
  }
}
</style>
